package com.zt.bus.line.controller;

import com.zt.bus.assistant.model.LineStop;
import com.zt.bus.constant.BusConstant;
import com.zt.bus.line.model.Line;
import com.zt.bus.line.service.LineService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/line")
public class LineController {

    @Autowired
    private LineService lineService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(lineService.getTable(reqBean));
    }

    @RequestMapping(value = "/catalogs")
    @ResponseBody
    public JSONObject catalogs(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(lineService.getCatalogs(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        lineService.add(getLine(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        lineService.update(getLine(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject deleteLine(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        lineService.delete(reqBean.getInfo("lineId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/stop/update")
    @ResponseBody
    public JSONObject updateAll(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        String lineId = reqBean.getInfo("lineId");
        String stopListStr = reqBean.getInfo("stopList");

        if (StringUtils.isBlank(lineId) || StringUtils.isBlank(stopListStr) || "[]".equals(stopListStr)) {
            throw new BusinessException("非法请求!");
        }

        List<LineStop> list = new ArrayList<LineStop>();
        // 处理线路站点
        JSONArray stopList = JSONArray.fromObject(stopListStr);
        if (stopList != null && stopList.size() > 0) {
            for (int i = 0; i < stopList.size(); i++) {
                JSONObject each = stopList.getJSONObject(i);
                LineStop lineStop = new LineStop();
                lineStop.setLineId(lineId);
                lineStop.setStopName(each.getString("stopName"));
                lineStop.setStopType(each.getString("stopType"));
                lineStop.setStopOrder(i);
                lineStop.setJingdu(Double.parseDouble(each.optString("jingdu")));
                lineStop.setWeidu(Double.parseDouble(each.optString("weidu")));
                lineStop.setAddress(each.optString("address"));
                lineStop.setRemark(each.optString("remark"));
                list.add(lineStop);
            }
        }
        lineService.updateStops(lineId, list);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/stops")
    @ResponseBody
    public JSONObject getStops(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(lineService.getStops(reqBean.getInfo("lineId")));
    }

    /**
     * 线路列表，提供给各客户端展示
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/customer/opened")
    @ResponseBody
    public JSONObject customerOpened(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(lineService.getCatalogLines(BusConstant.LineType.CUSTOME_BUS,
                BusConstant.LinePlanStatus.HAS_BEEN_OPENED));
    }

    /**
     * 信息招募，提供给各客户端展示
     * 
     * @return
     */
    @RequestMapping(value = "/customer/toBeOpen")
    @ResponseBody
    public JSONObject customerToBeOpen() {
        return Response.responseSuccess(lineService.getCatalogLines(BusConstant.LineType.CUSTOME_BUS,
                BusConstant.LinePlanStatus.TO_BE_OPEN));
    }

    /**
     * 线路列表，提供给各客户端展示
     * 
     * @return
     */
    @RequestMapping(value = "/employee/opened")
    @ResponseBody
    public JSONObject employeeOpened() {
        return Response.responseSuccess(lineService.getCatalogLines(BusConstant.LineType.EMPLOYEE_BUS,
                BusConstant.LinePlanStatus.HAS_BEEN_OPENED));
    }
    
    /**
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public JSONObject detail(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(lineService.getLineDetail(reqBean.getInfo("lineId"),reqBean.getInfo("planStatus")));
    }

    /**
     * 从表单获取对象
     * 
     * @param reqBean
     * @return
     */
    private Line getLine(ReqBean reqBean) {
        Line line = new Line();
        line.setLineId(reqBean.getInfo("lineId"));
        line.setLineNo(reqBean.getInfo("lineNo"));
        line.setLineName(reqBean.getInfo("lineName"));
        line.setCatalog(reqBean.getInfo("catalog"));
        line.setLineType(reqBean.getInfo("lineType"));
        line.setMileage(reqBean.getInfoDouble("mileage"));
        line.setPrice(reqBean.getInfoDouble("price"));
        line.setIntroduce(reqBean.getInfo("introduce"));
        line.setOperator(reqBean.getUserId());
        line.setRemark(reqBean.getInfo("remark"));
        return line;
    }

}
